package com.zoe.onelink.core.validation.strategy;

import cn.hutool.core.lang.Assert;
import com.zoe.onelink.common.enumeration.ExceptionCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 抽象校验策略
 */
public abstract class AbstractValidateStrategy<T> implements ValidateStrategy<T> {
    protected Logger logger = LoggerFactory.getLogger(AbstractValidateStrategy.class);


    protected AbstractValidateStrategy<T> beforeValidateStrategy;
  
    protected AbstractValidateStrategy() {
        beforeValidateStrategy = null;
    }
 
    protected AbstractValidateStrategy(AbstractValidateStrategy<T> beforeValidateStrategy) {
        this.beforeValidateStrategy = beforeValidateStrategy;
    }
  
    @Override
    public boolean valid(T target, Map<String, Object> contextMap) {
        if (this.beforeValidateStrategy != null && !beforeValidateStrategy.valid(target, contextMap)) {
            return false;
        }
        boolean result = this.valid(target);
        if (!result) {
            contextMap.put("Strategy", this.name());
        }
        Assert.notNull(this.name(),"[Assertion failed] - this strategyName is required; it must not be null");
        String strategyName = this.name();
        logger.debug("[{}] value:[{}],result:[{}]", strategyName, target, result?"success":"failure");
        return result;
    }
  
    protected abstract boolean valid(T  target);


    public abstract ExceptionCode message();

    @Override
    public String name() {
        return this.getClass().getSimpleName();
    }

}